package com.moss.cloud.common.core.utils;

import com.moss.cloud.common.core.exception.BaseException;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.handel.BranchHandle;
import com.moss.cloud.common.core.handel.BusinessExceptionHandel;
import com.moss.cloud.common.core.handel.ThrowExceptionHandel;
import com.moss.cloud.common.core.handel.TrueHandel;

public class BooleanHandel {
    /**
     *  如果参数为true抛出异常
     *
     * @param b
     * @return com.moss.cloud.common.core.handel.ThrowExceptionFunction
     **/
    public static ThrowExceptionHandel trueThrow(Boolean b){
        return (errorMessage) -> {
            if (b){
                throw new BaseException(errorMessage);
            }
        };
    }
    /**
     * 参数为true或false时，分别进行不同的操作
     *
     * @param b
     * @return com.moss.cloud.common.core.handel.BranchHandle
     **/
    public static BranchHandle isTureOrFalse(Boolean b){
        return (trueHandle, falseHandle) -> {
            if (b){
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }
    /**
     * 参数为true时进行操作
     *
     * @param b
     * @return com.moss.cloud.common.core.handel.TrueHandel
     **/
    public static TrueHandel isTrue(Boolean b){
        return (trueHandle) -> {
            if (b){
                trueHandle.run();
            }
        };
    }
    /**
     * 如果参数为true抛出异常
     * 主要是用于服务间调用抛出上游服务异常码和异常信息
     *
     * @param b
     * @return com.moss.cloud.common.core.handel.BusinessExceptionHandel
     **/
    public static BusinessExceptionHandel businessThrow(Boolean b) {
        return (codeMesg) -> {
            if (b) {
                throw new BaseException(SystemErrorType.BUSINESS_ERROR, codeMesg);
            }
        };
    }
}
